package com.zb.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.ZjStoreInboundMaterialCode;
import com.zb.mapper.ZjStoreInboundMaterialCodeMapper;
import com.zb.service.ZjStoreInboundMaterialCodeService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 入库物料一物一码表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2024-01-12
 */
@Service
public class ZjStoreInboundMaterialCodeServiceImpl extends ServiceImpl<ZjStoreInboundMaterialCodeMapper, ZjStoreInboundMaterialCode> implements ZjStoreInboundMaterialCodeService {


    // 根据入库ID删除入库物料一物一码
    @Override
    public void deleteByStoreInboundId(Long storeInboundId) {
        QueryWrapper<ZjStoreInboundMaterialCode> wrapper = new QueryWrapper<>();
        wrapper.eq("store_inbound_id", storeInboundId);
        this.remove(wrapper);
    }

    // 根据入库单ID查询入库物料一物一码
    @Override
    public List<ZjStoreInboundMaterialCode> getbyStoreInboundId(Long storeInboundId) {
        QueryWrapper<ZjStoreInboundMaterialCode> wrapper = new QueryWrapper<>();
        wrapper.select("id", "store_inbound_id", "store_inbound_material_id", "type", "code_id", "code");
        wrapper.eq("store_inbound_id", storeInboundId);
        return this.list(wrapper);
    }

    // 根据入库物料ID获取物料一物一码
    @Override
    public List<ZjStoreInboundMaterialCode> getbyStoreInboundMaterialsIds(List<Long> inboundMaterialIds) {
        if (CollectionUtil.isEmpty(inboundMaterialIds)) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjStoreInboundMaterialCode> wrapper = new QueryWrapper<>();
        wrapper.select("id", "store_inbound_id", "store_inbound_material_id", "type", "code_id", "code");
        wrapper.in("store_inbound_material_id", inboundMaterialIds);
        return this.list(wrapper);
    }

    // 根据一物一码ID删除入库物料的一物一码
    @Override
    public void deletebyCodeIds(List<Long> codeIds) {
        if (CollectionUtil.isEmpty(codeIds)) {
            return;
        }
        QueryWrapper<ZjStoreInboundMaterialCode> wrapper = new QueryWrapper<>();
        wrapper.in("code_id", codeIds);
        this.remove(wrapper);
    }

    // 根据删除的一物一码ID获取关联入库物料的一物一码
    @Override
    public List<String> getCodeByDelCodeIds(List<Long> delCodeIds) {
        if (CollectionUtil.isEmpty(delCodeIds)) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjStoreInboundMaterialCode> wrapper = new QueryWrapper<>();
        wrapper.last("limit 10");
        wrapper.select("code");
        wrapper.in("code_id", delCodeIds);
        return this.list(wrapper).stream().map(ZjStoreInboundMaterialCode::getCode).distinct().toList();
    }
}
